package com.vehicle_information_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehicle_information_management_system.dto.VehicleCount;
import com.vehicle_information_management_system.dto.VehicleDto;
import com.vehicle_information_management_system.dto.VehicleWithDriver;
import com.vehicle_information_management_system.pojo.VehicleInfo;
import com.vehicle_information_management_system.service.impl.VehicleInfoServiceImpl;
import com.vehicle_information_management_system.utils.Result;
<<<<<<< HEAD
=======
import lombok.extern.slf4j.Slf4j;
>>>>>>> 629a544 (update)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vehicleInfo")
<<<<<<< HEAD
=======
@Slf4j
>>>>>>> 629a544 (update)
public class VehicleInfoController {

    @Autowired
    private VehicleInfoServiceImpl vehicleInfoService;

    @RequestMapping("/list")
    public Result<?> list(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "") String search) {
<<<<<<< HEAD
        IPage<VehicleInfo> vehicleInfoIPage= vehicleInfoService.selectPage(pageNum,pageSize,search);
        return Result.success(vehicleInfoIPage);
=======
        IPage<VehicleInfo> vehicleInfoPage= vehicleInfoService.selectPage(pageNum,pageSize,search);
        return Result.success(vehicleInfoPage);
>>>>>>> 629a544 (update)
    }
    @RequestMapping("/add")
    public Result<?> add(@RequestBody VehicleInfo vehicleInfo) {
        try{
            vehicleInfoService.add(vehicleInfo);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
        return Result.success("添加成功");
    }
    @RequestMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        try{
            vehicleInfoService.delete(id);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
        return Result.success("删除成功");
    }
    @RequestMapping("/update")
    public Result<?> update(@RequestBody VehicleInfo vehicleInfo) {
        try{
            vehicleInfoService.update(vehicleInfo);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
        return Result.success("修改成功");
    }
    @RequestMapping("/getVehicleInfoByDepartment")
    public Result<?> getVehicleInfoByDepartment() {
        try{
            List<VehicleCount> vehicleInfoList = vehicleInfoService.getVehicleInfoByDepartment();
            return Result.success(vehicleInfoList);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }
    @RequestMapping("/getDriverByVehicle/{vehicleId}")
    public Result<?> getDriverByVehicle(@PathVariable Integer vehicleId)  {
        try{
            VehicleWithDriver vehicleInfo = vehicleInfoService.getVehicleWithDriver(vehicleId);
            return Result.success(vehicleInfo);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }

    @RequestMapping("/getNoUseVehicleInfo")
    public Result<?> getNoUseVehicleInfo()  {
        try{
            List<VehicleDto> vehicleInfoList = vehicleInfoService.getNoUseVehicleInfo();
            return Result.success(vehicleInfoList);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }
}
